//
//  UINavigationItem+Extend.m
//  WoSteward2
//
//  Created by leeqb on 12/31/13.
//
//

#import "UINavigationItem+Extend.h"

@implementation UINavigationItem (Extend)

- (void)setLeftBarButtonItem:(UIBarButtonItem *)_leftBarButtonItem
{
    UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        spaceButtonItem.width = -8;
    } else {
        spaceButtonItem.width = 4;
    }
    
    if (_leftBarButtonItem)
    {
        [self setLeftBarButtonItems:@[spaceButtonItem, _leftBarButtonItem]];
    }
    else
    {
        [self setLeftBarButtonItems:@[spaceButtonItem]];
    }
}

- (void)setRightBarButtonItem:(UIBarButtonItem *)_rightBarButtonItem
{
    UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        spaceButtonItem.width = -4;
    } else {
        spaceButtonItem.width = 4;
    }
    
    if (_rightBarButtonItem)
    {
        [self setRightBarButtonItems:@[spaceButtonItem, _rightBarButtonItem]];
    }
    else
    {
        [self setRightBarButtonItems:@[spaceButtonItem]];
    }
}

- (void)setBarCustonButtons:(NSArray *)_barButtonItems pos:(BOOL)isPos
{
    UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        spaceButtonItem.width = isPos ? -4 : -8;
    } else {
        spaceButtonItem.width = isPos ? 8 : 4;
    }
    
    if (_barButtonItems)
    {
        NSMutableArray *items = [NSMutableArray arrayWithObject:spaceButtonItem];
        [items addObjectsFromArray:_barButtonItems];
        [self setRightBarButtonItems:items];
    }
    else
    {
        [self setRightBarButtonItems:@[spaceButtonItem]];
    }
}

@end
